package Concurent;

import java.util.concurrent.CyclicBarrier;

/**
 * Created by root on 17-6-7.
 */
public class CyclicBarrierDemo1 {
    //栅栏，让进程都先准备好，然后同时执行

    public static void main(String[] args){
        CyclicBarrier cb = new CyclicBarrier(3);
        new Thread(new T6(cb)).start();
        new Thread(new T7(cb)).start();
        new Thread(new T8(cb)).start();
    }
}

class T6 implements Runnable{
    private CyclicBarrier cb = null;


    public T6(CyclicBarrier cb) {
        this.cb = cb;
        System.out.println(cb);
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);

            System.out.println("T6到达栅栏");
            cb.await();
            System.out.println("T6释放");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



class T7 implements Runnable{
    private CyclicBarrier cb = null;

    public T7(CyclicBarrier cb) {
        this.cb = cb;
    }



    @Override
    public void run() {
        try {

            Thread.sleep(2000);
            System.out.println("T7到达栅栏");
            cb.await();
            System.out.println("T7释放");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


class T8 implements Runnable{
    private CyclicBarrier cb = null;

    public T8(CyclicBarrier cb) {
        this.cb = cb;
    }



    @Override
    public void run() {
        try {
            Thread.sleep(3000);
            System.out.println("T8到达栅栏");
            cb.await();
            System.out.println("T8释放");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}